import router from './index'
import store from '../store'
import { getQueryString } from '../util/util'
import { checkneedauth, auth, checkpayneedauth, payauth, isNeedAuthorization, checkcashneedauth, getPayInfo, simulationLogin, mnlogin } from '../service/api'

const debug = process.env.NODE_ENV !== 'production'


var liangzongurl = 'https://api.hidlq.com';
var qumengurl = 'https://shop.hidlq.com';

if(debug){
    liangzongurl = 'http://localhost:38888';
}


async function be1 (to, from, next) {

    const company_code = getQueryString('company_code');
    store.commit('setCompanyCode', company_code);

    var u = navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

    if (isiOS) {
        store.commit('setIOSURL', window.location.href);
    }


    console.log(from);
    console.log(to);

    //首页和绑定微信页不校验
    if(to.path == '/home' 
    || to.path == '/bindwx' 
    //|| to.name == 'destroylogin' 
    || to.path == '/destroy/login.html' 
    || to.name == 'destroymain'
    || to.name == 'destroystatistics'
    || to.name == 'orderinfo'       //订单详情不校验是否登录
    || to.name == 'orderinfosms'       //订单详情不校验是否登录
    || to.name == 'toolmain'       
    || to.name == 'toolmine'
    ){
        if (to.meta.title) {
            document.title = to.meta.title;
        }
        next();
        return;
    }

   
    //生产
    if (!debug) {

        const para = {
            fun : 'checkneedauth1',
            requestType : 'get',
            cb : res => {
                console.log(res);
                if(res == '0'){ //不需要授权

                    //支付授权
                    if (to.matched.some(record => record.meta.requiresAuth)) {

                        const para1 = {
                            fun : 'checkpayneedauth1',
                            pay_company_code : to.query.pay_company_code,
                            requestType : 'get',
                            cb : res1 => {
                                console.log('checkpayneedauth1');
                                console.log(res1);

                                if(res1 == '1'){

                                    window.location.href = liangzongurl + '/v1/un/payauth?company_code=' + 
                                    company_code + '&pay_company_code=' + to.query.pay_company_code + '&rurl='+ 
                                    encodeURIComponent(qumengurl + '/#' + to.fullPath) + '&v=' + (new Date().getTime());
                                    return;
                                }
                            }
                        }

                        store.dispatch('update', para1);
                    }


                    //是否需要提现授权
                    if (to.matched.some(record => record.meta.requiresCashAuth)) {

                        const para2 = {
                            fun : 'checkcashneedauth1',
                            requestType : 'get',
                            cb : res1 => {
                                console.log('checkcashneedauth1');
                                console.log(res1);
                                if(res1 == '1'){
                                    window.location.href = liangzongurl + '/v1/un/cashauth?company_code=' + 
                                    company_code + '&rurl='+ 
                                    encodeURIComponent(qumengurl + '/#' + to.fullPath) + '&v=' + (new Date().getTime());
                                    return;
                                }
                            }
                        }

                        store.dispatch('update', para2);
                    }


                    // const userdataPara = {
                    //     fun : 'userData',
                    //     requestType : 'get',
                    //     cb : res => {
                    //         if (to.meta.title) {
                    //             document.title = to.meta.title;
                    //         }
                    //         next();
                    //     }
                    // };
                    // store.dispatch('query', userdataPara);

                    store.dispatch('getUserData', {
                        'company_code' : company_code,
                        'callback' : (res) => {
                            //document.body.scrollTop = 0;
                            //document.documentElement.scrollTop = 0; 
                            if (to.meta.title) {
                                document.title = to.meta.title;
                            }
                            next();
                        }
                    });
                    
                }else if(res == '1'){   //需要授权

                    next(false);

                    window.location.href = liangzongurl + '/v1/un/auth?company_code=' + 
                    company_code + '&rurl='+ 
                    encodeURIComponent(qumengurl + '/#' + to.fullPath) + '&v=' + (new Date().getTime());

                    return;
                }else{  //没有配置公众号
                    //this.$dialog.alert({'mes' : JSON.stringify(res)});
                }

            }
        };

        store.dispatch('update', para);


        //const res = await checkneedauth();
        //console.log(res);

        // // //需要授权
        // if(res.data == '1'){

        //     window.location.href = liangzongurl + '/v1/un/auth?company_code=' + 
        //     company_code + '&rurl='+ 
        //     encodeURIComponent(qumengurl + '/#' + to.fullPath) + '&v=' + (new Date().getTime());

        //     return;
        // }

        // //是否需要支付授权
        // if (to.matched.some(record => record.meta.requiresAuth)) {
        //     var res1 = await checkpayneedauth(to.query.pay_company_code);

        //     if(res1.data == '1'){
        //         console.log('需要静默授权');
        //         //n = false;
        //         //这里的company_code 应该从 产品中获取!!!!!!!!
        //         //const pay_company_code = 'LW88888'
        //         window.location.href = liangzongurl + '/v1/un/payauth?company_code=' + 
        //         company_code + '&pay_company_code=' + to.query.pay_company_code + '&rurl='+ 
        //         encodeURIComponent(qumengurl + '/#' + to.fullPath) + '&v=' + (new Date().getTime());

        //         return;
        //     }
        // }


        // //是否需要提现授权
        // if (to.matched.some(record => record.meta.requiresCashAuth)) {
        //     var res1 = await checkcashneedauth();
        //     if(res1.data == '1'){
        //         console.log('需要静默授权');
        //         //n = false;
        //         //这里的company_code 应该从 产品中获取!!!!!!!!
        //         //const pay_company_code = 'LW88888'
        //         window.location.href = liangzongurl + '/v1/un/cashauth?company_code=' + 
        //         company_code + '&rurl='+ 
        //         encodeURIComponent(qumengurl + '/#' + to.fullPath) + '&v=' + (new Date().getTime());

        //         return;
        //     }
        // }



        

    }else{  //测试
        const res = await mnlogin();
        console.log(res);

        store.dispatch('getUserData', {
            'company_code' : company_code,
            'callback' : (res) => {
                //document.body.scrollTop = 0;
                //document.documentElement.scrollTop = 0; 
                if (to.meta.title) {
                    document.title = to.meta.title;
                }
                next();
            }
        });
    }
    

    

    // const res = await auth(company_code);

    // console.log(res);

    //window.location.href = res.data;

}




// async function be (to, from, next) {

//     const company_code = getQueryString('company_code');
//     store.commit('setCompanyCode', company_code);

//     var n = true;
//     var res = await isNeedAuthorization();

//     //生产环境
//     if (!debug) {
//         //需要授权
//         if (res.status != '1') {
//             console.log('跳转授权页面');
//             n = false;
//             window.location.href = liangzongurl + '/Api/Api/fxauth/wxcode/' + 
//             company_code + '?rurl='+ 
//             encodeURIComponent(qumengurl + '/?#' + to.fullPath);
//         } else {
//             //是否需要支付授权
//             if (to.matched.some(record => record.meta.requiresAuth)) {
//                 var res1 = await getPayInfo();
//                 if(res1.status != '1'){
//                     console.log('需要静默授权');
//                     n = false;
//                     location.href = liangzongurl + '/Api/Api/payJmAuth/wxcode/' + 
//                     company_code + '?rurl='+ 
//                     encodeURIComponent(qumengurl + '/?#' + to.fullPath);
//                 }
//             }
//         }
//     } else {  //测试环境, 由于测试环境不能支付，不判断是否需要支付授权了。
//         if (res.status != '1') {  //需要授权
//             var res1 = await simulationLogin();
//         }
//     }

//     if(n){
        
//         store.dispatch('getUserData', {
//            'company_code' : company_code,
//             'callback' : (res) => {
//                 //document.body.scrollTop = 0;
//                 //document.documentElement.scrollTop = 0; 
//                 next();
//             }
//         });
//     }
// }

router.beforeEach(be1);





